﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using ECCIT.Business.Model;
using System.Linq;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DAL;
using OgilvyOne.UKNow.ServiceReference;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.BLL;
using Lucene.Net.Documents;

namespace OgilvyOne.UKNow.BLL
{
    public class SponsorBLL : IGenerateHtml
    {
        /*修改人：穆凤凤
      * 修改时间：2012-02-23
      * 修改内容：
      *      新增LanguageID属性和构造函数
      *      有mff注释的为本次修改的代码
      */
        //mff
        public int LanguageID { get; set; }
        public SponsorBLL(int langID)
        {
            this.LanguageID = langID;
        }
        //mff
        public IList<SponsorModel> selectSponsor()
        {
            IDALSponsor use = DALFactory.SponsorDALFactory.Create(LanguageID);
            return use.GetAllSponsorModels();
        }
        //根据赞助商sid 获取该赞助商信息
        public SponsorModel selSponsor(int sid)
        {
            IDALSponsor use = SponsorDALFactory.Create(LanguageID);
            return use.GetSponsorModelByID(sid);
        }
        //删除赞助商的数据
        public ResultInfo delSponsor(int eid)
        {
            IList<int?> list = new List<int?>();
            list.Add(eid);
            IDALSponsor use = SponsorDALFactory.Create(LanguageID);
            return use.Delete(list);
        }
        //修改单个赞助商的发布状态
        public void modPubOne(int sid)
        {
            List<int?> list = new List<int?>();
            list.Add(sid);

            IDALSponsor use = SponsorDALFactory.Create(LanguageID);

            SponsorModel model = use.GetSponsorModelByID(sid); //查看该赞助商的当前发布状态
            if (model.State > 0) //该关键字的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该赞助商的state为0 不发布 (传递参数 sid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该赞助商的state为1 发布     (传递参数 sid 和 int=1)
            }
        }
        //新增赞助商
        public ResultInfo Save(SponsorModel info)
        {
            IDALSponsor use = SponsorDALFactory.Create(LanguageID);
            return use.Save(info);
        }

        /// <summary>
        ///     生成活动详情页HTML
        /// </summary>
        /// <param name="model"></param>
        public ResultInfo GenerateHtml(BaseModel baseModel, string dir)
        {
            ResultInfo result = new ResultInfo(false);
            string html = GetHtml(baseModel);
            if (string.IsNullOrEmpty(html)) return result;

            try
            {
                string filename = System.Web.HttpContext.Current.Server.MapPath("~" + dir + "/") + ((SponsorModel)baseModel).HtmlFilePath;
                FileUtil.SaveHtmlFile(filename, html);
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        public string GetHtml(BaseModel baseModel)
        {
            string html = null;
            SponsorModel model = (SponsorModel)baseModel;
            if (model.TemplateID > 0)
            {
                //生成HTML
                CmsVoWrapper vo = new CmsVoWrapper();
                vo.LanguageID = this.LanguageID;
                vo.Aid = model.ID.Value;
                vo.IsHtmlFile = true;
                vo.SponsorModel = model;
                vo.Tid = model.TemplateID.Value;
                vo.Cid = model.ColumnID.Value;
                html = CmsProxy.GetHtml(vo);
            }

            return html;
        }

        public BaseModel GetModelByID(int id)
        {
            return selSponsor(id);
        }

        public IList<BaseModel> GetAllStaticPublishModels()
        {
            return selectSponsor().Cast<BaseModel>().ToList<BaseModel>();
        }

        public IList<Document> CreateLuceneDocuments()
        {
            IList<SponsorModel> models = selectSponsor();
            IList<Document> documents = new List<Document>();
            foreach (SponsorModel model in models)
            {
                Document doc = new Document();
                doc.Add(new Field("html", model.Detail, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("title", model.Name, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("url", string.Format("/{0}/{1}.html", (this.LanguageID == 2052) ? "cn" : "en", model.HtmlFilePath), Field.Store.YES, Field.Index.NOT_ANALYZED));
                documents.Add(doc);
            }

            return documents;
        }
    }
}

